import Taro from "@tarojs/taro";

type EventCallback<T = any> = (data?: T) => void;
export default class EventUtils {
  static UserInfo = 'refreshUserinfo'
  static ApplyRecharge = 'applyRecharge'
  static RefreshOrder = 'RefreshOrder'
  static RefreshOrderDetail = 'RefreshOrderDetail'
  static OK = 'ok'

  // 使用泛型指定数据类型，data 参数可选
  static on<T>(key: string, callback: EventCallback<T>) {
    // 在事件触发时，将 data 传递给回调函数
    Taro.eventCenter.on(key, (data?: T) => callback(data));
    return () => {
      Taro.eventCenter.off(key, callback as (data?: any) => void);
    };
  }

  // data 参数设置为可选
  static trigger<T>(key: string, data?: T) {
    Taro.eventCenter.trigger(key, data);
  }

}
